﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Qianwei.Model;
using Qianwei.Dac;
using Qianwei.Common;
using Qianwei.Common.Extension;
using MySql.Data.MySqlClient;

namespace Qianwei.Biz
{
    public class UserInfoManager : ManagerBase<UserInfoManager>
    {
        public IList<user_info> QueryUserInfo(string strWhere)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM user_info ");
            if (strWhere != null && strWhere.Trim().Length > 0)
            {
                strSql.Append(" where " + strWhere);
            }
            IList<user_info> uilist = Mapper.DataReaderToList<user_info>(msh.ExecuteReader(strSql.ToString()));
            return uilist;
        }

        public bool UserIsExists(string loginName, string password)
        {
            MySQLHelper msh = new MySQLHelper();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select 1 ");
            strSql.Append(" FROM user_info ");
            strSql.Append(" where Login_Name=@Login_Name");
            strSql.Append(" and Password=@Password");

            MySqlParameter[] parameters = {
					new MySqlParameter("@Login_Name", loginName),
					new MySqlParameter("@Password", password)};

            object obj = msh.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public int Add(user_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into user_info(");
            strSql.Append("Login_Name,User_Name,Register_Time,Last_Login_Time,Last_Login_IP)");
            strSql.Append(" values (");
            strSql.Append("@Login_Name,@User_Name,@Register_Time,@Last_Login_Time,@Last_Login_IP)");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Login_Name", model.Login_Name),
					new MySqlParameter("@User_Name", model.User_Name),
					new MySqlParameter("@Register_Time", model.Register_Time),
					new MySqlParameter("@Last_Login_Time", model.Last_Login_Time),
					new MySqlParameter("@Last_Login_IP", model.Last_Login_IP)};

            MySQLHelper msh = new MySQLHelper();
            object obj = msh.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public bool UpdatePassword(user_info model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update user_info ");
            strSql.Append(" set Password=@Password");
            strSql.Append(" where Login_Name=@Login_Name");
            MySqlParameter[] parameters = {
                new MySqlParameter("@Password", model.Password),
                new MySqlParameter("@Login_Name", model.Login_Name)
            };
            MySQLHelper msh = new MySQLHelper();
            int rows = msh.ExecuteSql(strSql.ToString(), parameters);
            return rows > 0;
        }

        public bool UpdateLoginInfo(string loginName, string ip)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update user_info set ");
            strSql.Append("Last_Login_Time=@Last_Login_Time,");
            strSql.Append("Last_Login_IP=@Last_Login_IP");
            strSql.Append(" where Login_Name=@Login_Name");
            MySqlParameter[] parameters = {
					new MySqlParameter("@Login_Name", loginName),
					new MySqlParameter("@Last_Login_Time", DateTime.Now),
					new MySqlParameter("@Last_Login_IP", ip)};

            MySQLHelper msh = new MySQLHelper();
            int rows = msh.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
